/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.soccergamesarchive.validators;

import cz.muni.fi.pa165.soccergamesarchive.persistencelayer.entities.Goal;

import static cz.muni.fi.pa165.soccergamesarchive.errorhandling.ErrorHandler.INCOMPLETE_RECORD_ERROR;
import static cz.muni.fi.pa165.soccergamesarchive.errorhandling.ErrorHandler.INCORRECT_ID_ERROR;
import static cz.muni.fi.pa165.soccergamesarchive.errorhandling.ErrorHandler.NO_ERROR;
import static cz.muni.fi.pa165.soccergamesarchive.errorhandling.ErrorHandler.NULL_VALUE_ERROR;
import static cz.muni.fi.pa165.soccergamesarchive.errorhandling.ErrorHandler.RUNTIME_ERROR;
import static cz.muni.fi.pa165.soccergamesarchive.errorhandling.ErrorHandler.createErrorMessage;

/**
 *
 * @author tomas plevko
 */
public class GoalValidator {

    public static int validateGoal(Goal goal, ValidatorTest... tests) throws
            RuntimeException {

        for (ValidatorTest test : tests) {

            switch (test) {
                case NULL_TEST:
                    executeNullTest(goal);
                    break;
                case ID_TEST:
                    executeIDTest(goal);
                    break;
                case CORRECT_RECORD_TEST:
                    executeCorrectRecordTest(goal);
                    break;
                case FULL_TEST:
                    executeFullTest(goal);
                    break;
                default:
                    System.err.println("Undefined test: " + test);
                    return RUNTIME_ERROR;
            }
        }

        return NO_ERROR;
    }

    private static void executeNullTest(Goal goal) throws
            IllegalArgumentException {

        if (goal == null) {
            throw new IllegalArgumentException(createErrorMessage(
                    GoalValidator.class.getName(), NULL_VALUE_ERROR));
        }
    }

    private static void executeIDTest(Goal goal) throws
            IllegalArgumentException {

        if (goal.getId() == null || goal.getId() < 1) {

            throw new IllegalArgumentException(createErrorMessage(
                    GoalValidator.class.getName(), INCORRECT_ID_ERROR));
        }
    }

    private static void executeCorrectRecordTest(Goal goal) throws
            IllegalArgumentException {
        if (goal.getPlayer() == null || goal.getSoccerMatch() == null || goal
                .getScoreTime() < 0) {

            throw new IllegalArgumentException(createErrorMessage(
                    GoalValidator.class.getName(), INCOMPLETE_RECORD_ERROR));
        }
    }

    private static void executeFullTest(Goal goal) throws
            IllegalArgumentException {
        executeNullTest(goal);
        executeIDTest(goal);
        executeCorrectRecordTest(goal);
    }

}
